/**
 * Copyright (c) 2007, Blue Hole Software. All rights reserved.
 * Code licensed under the Apache 2.0 License:
 * http://www.apache.org/licenses/
 */
package org.bhf.providers.security.authentication;

import org.bhf.json.JSONParser;
import org.bhf.json.JSONWriter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Map a user or role to a role.
 */
@Entity
@Table( name = "ROLE_MAP" )
public class RoleMap
{
    private long                    id;
    private Role                    role;
    private String                  project;
    private Map<String,String[]>    parameters = new HashMap<String,String[]>();

    static final private Type   MapStringString;
    static
    {
        try
        {
            MapStringString = RoleMap.class.getMethod( "getParameters" ).getGenericReturnType();
        }
        catch( NoSuchMethodException nsme )
        {
            throw new NoSuchMethodError();
        }
    }

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    public long                         getId()
    {
        return id;
    }

    public void                         setId( final long id )
    {
        this.id = id;
    }

    @ManyToOne( optional = false )
    public Role                         getRole()
    {
        return role;
    }

    public void                         setRole( final Role role )
    {
        this.role = role;
    }

    public String                       getProject()
    {
        return project;
    }

    public void                 setProject( final String project )
    {
        this.project = project;
    }

    /**
     *
     * @return JSON
     * @exclude
     */
    @Column( name = "PARAMETERS" )
    @Lob
    public String               getPersistentParameters()
    {
        try { return new JSONWriter().writeObject( parameters ); } catch( IOException ioe ) { throw new RuntimeException( ioe ); }
    }

    /**
     *
     * @param parameters JSON
     * @throws IOException Write error
     * @exclude
     */
    public void                 setPersistentParameters( final String parameters )
        throws IOException
    {
        final JSONParser reader = new JSONParser();
        this.parameters = new HashMap<String,String[]>();
        reader.parseObject( MapStringString, this.parameters, parameters );
    }

    @Transient
    public Map<String,String[]> getParameters()
    {
        getPersistentParameters();
        return parameters;
    }

    public void                 setParameters( final Map<String,String[]> parameters )
    {
        assert parameters != null;

        this.parameters = parameters;
    }
}
